package io.github.iogogogo.commons.collection;

import io.github.iogogogo.commons.support.wrapper.PageWrapper;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by tao.zeng on 2024/11/7.
 */
public class ListUtils {

    public static <T> PageWrapper<T> partition(int pageNo, int pageSize, List<T> records) {
        if (pageNo <= 0) pageNo = 1;

        // 计算偏移量
        int offset = (pageNo - 1) * pageSize;

        // 总算总记录数
        int totalSize = records.size();

        if (offset > totalSize) return new PageWrapper<>(pageNo, pageSize, totalSize, Collections.emptyList());

        // 分页结果数据
        List<T> collect = records.stream().skip(offset).limit(pageSize).collect(Collectors.toList());

        return new PageWrapper<>(pageNo, pageSize, totalSize, collect);
    }
}
